*This file produces the results for Appendix Table A3, where we report the test statistics for alternative first-stage regressions.
*Note: You must hand copy the F_effective statistics into CSV file.

********************************************************************************
*DEFINE DIRECTORIES
local home CHILD
local main CHILD/JPE
local logs CHILD/JPE/logs
local data CHILD/JPE/data
local results CHILD/JPE/results
local network NETWORK
********************************************************************************

***************************************************************run regressions
*generate log file
cap log close
local time = subinstr(c(current_date)," ","",.)
cd "`results'"
log using table4.txt, text replace

*bring in data
cd "`main'"
use regression_data_final_jpe.dta, clear

*merge in single treatment variable
preserve
use all_summary_statistics.dta, clear
keep patientid treated-badfda nd adhd
tempfile temp
save `temp',replace
restore

drop treat
drop drug
drop bad

merge 1:1 patientid using `temp'
keep if _merge==3
drop _merge

*get rid of extra variables
drop tca benzo badfda

*define drug treatment outcome
generate drugs = drug_only+both>0
drop drug_only therapy_only both

*merge in new bad prescribing variables
merge 1:1 patientid using bad_prescribing2.dta
foreach var of varlist fda_ok-red_flag {
	replace `var' = 0 if `var'==.
}
drop _merge

*reorder variables
order treated drugs red_flag grey_area fda_ok, after(patientid)

**incorporate ND diagnosis
drop adhd nd
cd "`home'"
merge 1:1 patientid using neurodevelopmental.dta
keep if _merge==3
drop _merge

*add hurting yourself outcome
cd "`data'"
merge 1:1 patientid using hurting_yourself.dta

*recode kids who don't have hurting yourself records; and hard code to 1's if more than 1
foreach var of varlist before-second_year {
	replace `var' = 0 if `var'==.
	replace `var' = 1 if `var'>1
}
drop _merge

*merge in REVISED DIAGNOSIS INFO
cd "`home'"
merge 1:1 patientid using diagnosis_revised_final.dta
keep if _merge==3
drop _merge

*save final sample
preserve
cd "`data'"
keep patientid zcta_kid zip3 year treated drugs red_flag grey_area fda_ok
save final_jpe_sample.dta, replace
restore

*merge in additional instrument
destring zcta_kid, replace
cd "`home'"
merge m:1 zcta_kid year using market-measures-antipsych.dta
drop if _merge==2
drop _merge

*look for which leave out measure needed
cd "`data'"
merge 1:1 patientid using status_of_leave_out.dta
drop _merge

generate share_ap = share_antipsych
replace share_ap = share_anti_yes if antipsychotic==1
replace share_ap = share_anti_no if antipsychotic==0

*define variables (for lasso/tsls)
local instruments share_mh share_psych share_gp s1_by_psych share_ap
local severity first_hospital first_er first_eval in_hospital_pre
local other female month year

*diagnosis vectors
local diagnosis_int depression-any_multiple only_depression-formal_multiple nd
local diagnosis_control depression anxiety adjustment_anxiety adjustment_depression adjustment_dax  nd

*set outcome
local outcome tot3m

eststo clear

*run first-stage regressions for branch 1 (treat or not)
local var treat

*list the instruments from LASSO
local ins3 1.any_multiple#c.s1_by_psych 1.only_anxiety#c.s1_by_psych 1.any_multiple#c.share_mh  
local ins_all 1.any_multiple#c.share_mh 1.any_multiple#c.s1_by_psych 1.any_multiple#c.share_ap 1.only_anxiety#c.s1_by_psych

eststo:   quietly areg `var' i.(`other') age i.(`severity') i.(`diagnosis_control'), absorb(zip3) cluster(zip3)
estadd scalar Fff = 0

eststo:   quietly areg `var' i.(`other') age i.(`severity') i.(`diagnosis_control') c.share_mh##c.share_mh c.share_psych##c.share_psych c.share_gp##c.share_gp c.s1_by_psych##c.s1_by_psych c.share_ap##c.share_ap, absorb(zip3) cluster(zip3)
quietly ivreg2 `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') (`var'=c.share_mh##c.share_mh c.share_psych##c.share_psych c.share_gp##c.share_gp c.s1_by_psych##c.s1_by_psych) i.zip3 , cluster(zip3) 
weakivtest
estadd scalar Fff = r(F_eff)

eststo:   quietly areg `var' i.(`other') i.age i.(`severity') i.(`diagnosis_control')  i.age#c.(`instruments') i1.(`severity' `diagnosis_int' female)#c.(`instruments') c.(`instruments')##c.(`instruments'), absorb(zip3) cluster(zip3)
quietly ivregress 2sls `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') (`var'=i.age#c.(`instruments') i1.(`severity' `diagnosis_int' female)#c.(`instruments') c.(`instruments')##c.(`instruments')) i.zip3 ,  cluster(zip3) 
weakivtest
estadd scalar Fff = r(F_eff)

eststo:   quietly areg `var' i.(`other') age i.(`severity') i.(`diagnosis_control') `ins3' , absorb(zip3) cluster(zip3)
quietly ivreg2 `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') (`var'=`ins3') i.zip3 ,  cluster(zip3) 
weakivtest
estadd scalar Fff = r(F_eff)

eststo:   quietly areg `var' i.(`other') age i.(`severity') i.(`diagnosis_control') `ins_all' , absorb(zip3) cluster(zip3)
quietly ivreg2 `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') (`var'=`ins_all') i.zip3 ,  cluster(zip3) 
weakivtest
estadd scalar Fff = r(F_eff)

esttab using table4.csv, drop(*) star(* 0.10 ** 0.05 *** 0.01) se mtitles("OLS - No Instruments" "Main Instruments + Quadratic" "Full Interactions" "Post-Lasso: 3 Instruments" "Post-Lasso: All Instruments") nonumber title("Any Follow-Up Treatment in 3 Months After 1st Claim") replace nonotes stats(N r2 Fff, fmt(%9.0f %9.3f %9.1f))


********
********
keep if treated
********
********

eststo clear

*run first-stage regressions for branch 2 (drugs)
local var drugs
local ins3 1.first_eval#c.share_mh share_gp 1.any_anxiety#c.share_mh     
local ins_all 1.first_eval#c.share_mh 1.first_eval#c.share_gp 1.anxiety#c.share_gp

eststo:   quietly areg `var' i.(`other') age i.(`severity') i.(`diagnosis_control'), absorb(zip3) cluster(zip3)
estadd scalar Fff = 0

eststo:   quietly areg `var' i.(`other') age i.(`severity') i.(`diagnosis_control') c.share_mh##c.share_mh c.share_psych##c.share_psych c.share_gp##c.share_gp c.s1_by_psych##c.s1_by_psych c.share_ap##c.share_ap, absorb(zip3) cluster(zip3)
quietly ivreg2 `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') (`var'=c.share_mh##c.share_mh c.share_psych##c.share_psych c.share_gp##c.share_gp c.s1_by_psych##c.s1_by_psych) i.zip3 ,  cluster(zip3) 
weakivtest
estadd scalar Fff = r(F_eff)

eststo:   quietly areg `var' i.(`other') i.age i.(`severity') i.(`diagnosis_control')  i.age#c.(`instruments') i1.(`severity' `diagnosis_int' female)#c.(`instruments') c.(`instruments')##c.(`instruments'), absorb(zip3) cluster(zip3)
quietly ivregress 2sls `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') (`var'=i.age#c.(`instruments') i1.(`severity' `diagnosis_int' female)#c.(`instruments') c.(`instruments')##c.(`instruments')) i.zip3 ,  cluster(zip3) 
weakivtest
estadd scalar Fff = r(F_eff)

eststo:   quietly areg `var' i.(`other') age i.(`severity') i.(`diagnosis_control') `ins3' , absorb(zip3) cluster(zip3)
quietly ivreg2 `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') (`var'=`ins3') i.zip3 ,  cluster(zip3) 
weakivtest
estadd scalar Fff = r(F_eff)

eststo:   quietly areg `var' i.(`other') age i.(`severity') i.(`diagnosis_control') `ins_all' , absorb(zip3) cluster(zip3)
quietly ivreg2 `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') (`var'=`ins_all') i.zip3 ,  cluster(zip3) 
weakivtest
estadd scalar Fff = r(F_eff)

esttab using table4.csv, drop(*) star(* 0.10 ** 0.05 *** 0.01) se mtitles("OLS - No Instruments" "Main Instruments + Quadratic" "Full Interactions" "Post-Lasso: 3 Instruments" "Post-Lasso: All Instruments") nonumber title("Drug Treatment in 1st 3 Months After 1st Claim") append nonotes stats(N r2 Fff, fmt(%9.0f %9.3f %9.1f))

*******
*******
keep if drugs
*******
*******

eststo clear

*effect of red-flag drug treatment
local var red_flag
local ins3 1.only_adjustment#c.share_ap 1.only_adjustment#c.share_gp  1.only_adjustment#c.s1_by_psych   
local ins_all 1.any_depression#c.share_psych 1.only_adjustment#c.share_gp 1.only_adjustment#c.share_ap

eststo:   quietly areg `var' i.(`other') age i.(`severity') i.(`diagnosis_control'), absorb(zip3) cluster(zip3)
estadd scalar Fff = 0

eststo:   quietly areg `var' i.(`other') age i.(`severity') i.(`diagnosis_control') c.share_mh##c.share_mh c.share_psych##c.share_psych c.share_gp##c.share_gp c.s1_by_psych##c.s1_by_psych c.share_ap##c.share_ap, absorb(zip3) cluster(zip3)
quietly ivreg2 `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') (`var'=c.share_mh##c.share_mh c.share_psych##c.share_psych c.share_gp##c.share_gp c.s1_by_psych##c.s1_by_psych) i.zip3 ,  cluster(zip3) 
weakivtest
estadd scalar Fff = r(F_eff)

eststo:   quietly areg `var' i.(`other') i.age i.(`severity') i.(`diagnosis_control')  i.age#c.(`instruments') i1.(`severity' `diagnosis_int' female)#c.(`instruments') c.(`instruments')##c.(`instruments'), absorb(zip3) cluster(zip3)
quietly ivregress 2sls `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') (`var'=i.age#c.(`instruments') i1.(`severity' `diagnosis_int' female)#c.(`instruments') c.(`instruments')##c.(`instruments')) i.zip3 ,  cluster(zip3) 
weakivtest
estadd scalar Fff = r(F_eff)

eststo:   quietly areg `var' i.(`other') age i.(`severity') i.(`diagnosis_control') `ins3' , absorb(zip3) cluster(zip3)
quietly ivreg2 `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') (`var'=`ins3') i.zip3 ,  cluster(zip3) 
weakivtest
estadd scalar Fff = r(F_eff)

eststo:   quietly areg `var' i.(`other') age i.(`severity') i.(`diagnosis_control') `ins_all' , absorb(zip3) cluster(zip3)
quietly ivreg2 `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') (`var'=`ins_all') i.zip3 ,  cluster(zip3) 
weakivtest
estadd scalar Fff = r(F_eff)

esttab using table4.csv, drop(*) star(* 0.10 ** 0.05 *** 0.01) se mtitles("OLS - No Instruments" "Main Instruments + Quadratic" "Full Interactions" "Post-Lasso: 3 Instruments" "Post-Lasso: All Instruments") nonumber title("Red-flag Drug Treatment in 1st 3 Months After 1st Claim") append nonotes stats(N r2 Fff, fmt(%9.0f %9.3f %9.1f))

eststo clear

*effect of grey-area drug treatment
local var grey_area
local ins3 1.only_adjustment#c.share_gp 1.only_adjustment#c.share_psych  1.only_adjustment#c.s1_by_psych  
local ins_all 1.any_depression#c.share_psych 1.any_depression#c.share_gp 1.any_depression#c.share_ap 1.any_multiple#c.share_mh 1.only_anxiety#c.share_gp 1.only_adjustment#c.share_gp

eststo:   quietly areg `var' i.(`other') age i.(`severity') i.(`diagnosis_control'), absorb(zip3) cluster(zip3)
estadd scalar Fff = 0

eststo:   quietly areg `var' i.(`other') age i.(`severity') i.(`diagnosis_control') c.share_mh##c.share_mh c.share_psych##c.share_psych c.share_gp##c.share_gp c.s1_by_psych##c.s1_by_psych c.share_ap##c.share_ap, absorb(zip3) cluster(zip3)
quietly ivreg2 `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') (`var'=c.share_mh##c.share_mh c.share_psych##c.share_psych c.share_gp##c.share_gp c.s1_by_psych##c.s1_by_psych) i.zip3 ,  cluster(zip3) 
weakivtest
estadd scalar Fff = r(F_eff)

eststo:   quietly areg `var' i.(`other') i.age i.(`severity') i.(`diagnosis_control')  i.age#c.(`instruments') i1.(`severity' `diagnosis_int' female)#c.(`instruments') c.(`instruments')##c.(`instruments'), absorb(zip3) cluster(zip3)
quietly ivregress 2sls `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') (`var'=i.age#c.(`instruments') i1.(`severity' `diagnosis_int' female)#c.(`instruments') c.(`instruments')##c.(`instruments')) i.zip3 ,  cluster(zip3) 
weakivtest
estadd scalar Fff = r(F_eff)

eststo:   quietly areg `var' i.(`other') age i.(`severity') i.(`diagnosis_control') `ins3' , absorb(zip3) cluster(zip3)
quietly ivreg2 `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') (`var'=`ins3') i.zip3 ,  cluster(zip3) 
weakivtest
estadd scalar Fff = r(F_eff)

eststo:   quietly areg `var' i.(`other') age i.(`severity') i.(`diagnosis_control') `ins_all' , absorb(zip3) cluster(zip3)
quietly ivreg2 `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') (`var'=`ins_all') i.zip3 ,  cluster(zip3) 
weakivtest
estadd scalar Fff = r(F_eff)

esttab using table4.csv, drop(*) star(* 0.10 ** 0.05 *** 0.01) se mtitles("OLS - No Instruments" "Main Instruments + Quadratic" "Full Interactions" "Post-Lasso: 3 Instruments" "Post-Lasso: All Instruments") nonumber title("Grey-Area Drug Treatment in 1st 3 Months After 1st Claim") append nonotes stats(N r2 Fff, fmt(%9.0f %9.3f %9.1f))

eststo clear

*effect of fda-ok drug treatment
local var fda_ok
local ins3 1.any_depression#c.share_mh  c.share_psych#c.share_gp  1.any_multiple#c.share_mh   
local ins_all 1.any_depression#c.share_mh

eststo:   quietly areg `var' i.(`other') age i.(`severity') i.(`diagnosis_control'), absorb(zip3) cluster(zip3)
estadd scalar Fff = 0

eststo:   quietly areg `var' i.(`other') age i.(`severity') i.(`diagnosis_control') c.share_mh##c.share_mh c.share_psych##c.share_psych c.share_gp##c.share_gp c.s1_by_psych##c.s1_by_psych c.share_ap##c.share_ap, absorb(zip3) cluster(zip3)
quietly ivreg2 `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') (`var'=c.share_mh##c.share_mh c.share_psych##c.share_psych c.share_gp##c.share_gp c.s1_by_psych##c.s1_by_psych) i.zip3 ,  cluster(zip3) 
weakivtest
estadd scalar Fff = r(F_eff)

eststo:   quietly areg `var' i.(`other') i.age i.(`severity') i.(`diagnosis_control')  i.age#c.(`instruments') i1.(`severity' `diagnosis_int' female)#c.(`instruments') c.(`instruments')##c.(`instruments'), absorb(zip3) cluster(zip3)
quietly ivregress 2sls `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') (`var'=i.age#c.(`instruments') i1.(`severity' `diagnosis_int' female)#c.(`instruments') c.(`instruments')##c.(`instruments')) i.zip3 ,  cluster(zip3) 
weakivtest
estadd scalar Fff = r(F_eff)

eststo:   quietly areg `var' i.(`other') age i.(`severity') i.(`diagnosis_control') `ins3' , absorb(zip3) cluster(zip3)
quietly ivreg2 `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') (`var'=`ins3') i.zip3 ,  cluster(zip3) 
weakivtest
estadd scalar Fff = r(F_eff)

eststo:   quietly areg `var' i.(`other') age i.(`severity') i.(`diagnosis_control') `ins_all' , absorb(zip3) cluster(zip3)
quietly ivreg2 `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') (`var'=`ins_all') i.zip3 ,  cluster(zip3) 
weakivtest
estadd scalar Fff = r(F_eff)

esttab using table4.csv, drop(*) star(* 0.10 ** 0.05 *** 0.01) se mtitles("OLS - No Instruments" "Main Instruments + Quadratic" "Full Interactions" "Post-Lasso: 3 Instruments" "Post-Lasso: All Instruments") nonumber title("FDA-Approved Drug Treatment in 1st 3 Months After 1st Claim") append nonotes stats(N r2 Fff, fmt(%9.0f %9.3f %9.1f))

cap log close